package com.byb.server.service.impl;

import com.byb.server.service.IllnessInfoService;
import com.byb.server.domain.IllnessInfo;
import com.byb.server.repository.IllnessInfoRepository;
import com.byb.server.repository.search.IllnessInfoSearchRepository;
import com.byb.server.service.dto.IllnessInfoDTO;
import com.byb.server.service.dto.IllnessInfoTypeDTO;
import com.byb.server.service.mapper.IllnessInfoMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;
import java.util.stream.Collectors;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing IllnessInfo.
 */
@Service
@Transactional
public class IllnessInfoServiceImpl implements IllnessInfoService{

    private final Logger log = LoggerFactory.getLogger(IllnessInfoServiceImpl.class);

    private final IllnessInfoRepository illnessInfoRepository;

    private final IllnessInfoMapper illnessInfoMapper;

    private final IllnessInfoSearchRepository illnessInfoSearchRepository;
    public IllnessInfoServiceImpl(IllnessInfoRepository illnessInfoRepository, IllnessInfoMapper illnessInfoMapper, IllnessInfoSearchRepository illnessInfoSearchRepository) {
        this.illnessInfoRepository = illnessInfoRepository;
        this.illnessInfoMapper = illnessInfoMapper;
        this.illnessInfoSearchRepository = illnessInfoSearchRepository;
    }

    /**
     * Save a illnessInfo.
     *
     * @param illnessInfoDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public IllnessInfoDTO save(IllnessInfoDTO illnessInfoDTO) {
        log.debug("Request to save IllnessInfo : {}", illnessInfoDTO);
        IllnessInfo illnessInfo = illnessInfoMapper.toEntity(illnessInfoDTO);
        illnessInfo = illnessInfoRepository.save(illnessInfo);
        IllnessInfoDTO result = illnessInfoMapper.toDto(illnessInfo);
        //illnessInfoSearchRepository.save(illnessInfo);
        return result;
    }

    /**
     *  Get all the illnessInfos.
     *
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<IllnessInfoDTO> findAll(Pageable pageable) {
        log.debug("Request to get all IllnessInfos");
        return illnessInfoRepository.findAll(pageable)
            .map(illnessInfoMapper::toDto);
    }

    /**
     *  Get one illnessInfo by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public IllnessInfoDTO findOne(Long id) {
        log.debug("Request to get IllnessInfo : {}", id);
        IllnessInfo illnessInfo = illnessInfoRepository.findOne(id);
        return illnessInfoMapper.toDto(illnessInfo);
    }

    /**
     *  Delete the  illnessInfo by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete IllnessInfo : {}", id);
        illnessInfoRepository.delete(id);
       // illnessInfoSearchRepository.delete(id);
    }

    /**
     * Search for the illnessInfo corresponding to the query.
     *
     *  @param query the query of the search
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<IllnessInfoDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of IllnessInfos for query {}", query);
        Page<IllnessInfo> result = illnessInfoSearchRepository.search(queryStringQuery(query), pageable);
        return result.map(illnessInfoMapper::toDto);
    }

    /**
     *  根据type  选择查询数据 在进行拼装
     *  type 一般为0 ，1  如为0 在查询0的数据后，查询2的数据，然，拼装起来
     * @param id
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public IllnessInfoTypeDTO findAllByIdAndtypeId(Long id,Long type) {
        IllnessInfoTypeDTO result=null;
        if(type==70){
            result =   new IllnessInfoTypeDTO(illnessInfoRepository.findAllByProductIdAndTypeOfCaseId(id,type-1).stream().map(illnessInfoMapper::toDto).collect(Collectors.toList()),
                illnessInfoRepository.findAllByProductIdAndTypeOfCaseId(id,type).stream().map(illnessInfoMapper::toDto).collect(Collectors.toList()));
        }else if(type==71){
            result =   new IllnessInfoTypeDTO(null, illnessInfoRepository.findAllByProductIdAndTypeOfCaseId(id,type).stream().map(illnessInfoMapper::toDto).collect(Collectors.toList()));
        }else{
            return null;
        }
        return result;
    }

    @Override
    public boolean saveAll(List<IllnessInfoDTO> list) {
        log.debug("Request to save IllnessInfoForExcelDTO : {}", list);
        illnessInfoRepository.save(list.stream().map(illnessInfoMapper::toEntity).collect(Collectors.toList()));
        return true;
    }
}
